using System;
using System.ComponentModel;
using System.Net;
using System.Text;
using SF.Code.Extension.HttpHelper.Downloader;

namespace SG.Ling.AutoTranslator.Translation
{
    public class TranslationConverter : Component, IWebDataConverter<TranslationKey, TranslationTuple, TranslationResultEventArgs>
    {
        public TranslationConverter()
            : this(null)
        {
            
        }

        public TranslationConverter(IContainer container)
        {
            if (container != null)
                container.Add(this);
        }

        #region Implementation of IWebDataConverter<TranslationKey,TranslationTuple,out TranslationResultEventArgs>

        public TranslationTuple ToResult(byte[] bytes, ContentMetaData metaData)
        {
            return BatchTranslatorApi.ExtractTranslation(bytes, metaData);
        }

        public WebClient CreateWebClientPrototype()
        {
            return new WebClient
                {
                    BaseAddress = "http://translate.google.com",
                    Encoding = Encoding.UTF8,
                };
        }

        public Uri KeyToUri(ref TranslationKey key)
        {
            return key.GetTranslateUri();
        }

        public TranslationResultEventArgs CreateEventArgs(TranslationTuple result, bool fromCache)
        {
            return new TranslationResultEventArgs(result, fromCache);
        }
        
        #endregion
    }
}